#include <memory>
#include "TcpServer.hpp"
#include "ServerCal.hpp"

void OnMessage(std::shared_ptr<Connection> connection)
{
    ServerCal calculator;
    std::shared_ptr<TcpServer> server = connection->_tcp_server_str.lock(); // 通过 weak_ptr 获取 shared_ptr
 
    std::cout << "上层得到了数据： " << connection->Inbuffer() << std::endl;
    std::string response_n = calculator.Calculator(connection->Inbuffer());
    if(response_n.empty()) return;
    // 将得到的数据发送出去
    connection->AppendOutbuffer(response_n);

    server->Sender(connection); // 使用 shared_ptr 调用 Sender
}
    
int main()
{
    std::shared_ptr<TcpServer> reactor_ptr(new TcpServer(8888,OnMessage));
    reactor_ptr->Init();
    reactor_ptr->Start();
    return 0;
}